package one.stand.event;

import lombok.extern.slf4j.Slf4j;
import one.stand.service.GoodsSelectServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@Async("asyncServiceExecutor")
@Slf4j
public class UpdateGoodsEventListener {
    @Autowired
    private GoodsSelectServcie goodsSelectServcie;

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void hanldeEvent(UpdateGoodsEvent event) {
        log.info("update goods event");
        goodsSelectServcie.updateGoods(event.getSelectCompanyId(), event.getGoodsId());
    }

    @EventListener
    public void hanldeEvent(UpdateGoodsAsyncEvent event) {
        log.info("update goods event");
        goodsSelectServcie.updateGoods(event.getSelectCompanyId(), event.getGoodsId());
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void hanldeEvent(UpdateGoodsTypeEvent event) {
        log.info("update goods type event");
        goodsSelectServcie.updateType(event.getSelectCompanyId());
    }
}
